package com.workcell.workdata.xthings.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.CodeChainnumVo;
import com.workcell.workdata.xthings.entity.BaseTimeClock;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeTimeClock;
import com.workcell.workdata.xthings.mapper.CodeTimeClockMapper;
import com.workcell.workdata.xthings.service.BaseTimeClockService;
import com.workcell.workdata.xthings.service.CodeTimeClockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 码库考勤机信息表 服务实现类
 *
 * @author zhangguangrui
 * @since 2023-03-16
 */
@Service
public class CodeTimeClockServiceImpl extends ServiceImpl<CodeTimeClockMapper, CodeTimeClock> implements CodeTimeClockService {

    @Resource
    private BaseTimeClockService baseTimeClockService;

    /**
     * 保存二维码基本快照信息
     * @param codeChainnum
     * @return
     */
    public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum, CodeChainnumVo.SaveChainNumVo saveChainNumVo) {
        // 查询硬件信息
        LambdaQueryWrapper<BaseTimeClock> baseCondition = new QueryWrapper<BaseTimeClock>().lambda()
                .eq(BaseTimeClock::getId,Long.valueOf(codeChainnum.getBaseId()));
        BaseTimeClock baseFlowmeter = baseTimeClockService.getOne(baseCondition);
        if (ObjectUtil.isEmpty(baseFlowmeter)){
            return false;
        }
        // 添加二维码快照信息
        CodeTimeClock saveBean = new CodeTimeClock();
        BeanUtil.copyProperties(baseFlowmeter,saveBean, "createBy","createTime","updateBy","updateTime");
        saveBean.setCodeId(codeChainnum.getCodeId());
        saveBean.setChainNum(codeChainnum.getChainNum());
        saveBean.setDeviceNo(codeChainnum.getDeviceNo());
        saveBean.setTcId(Integer.valueOf(codeChainnum.getBaseId()));
        saveBean.setName(codeChainnum.getHardwareName());
        //添加边缘服务器参数
        saveBean.setIp(saveChainNumVo.getIp());
        saveBean.setPort(saveChainNumVo.getPort());
        saveBean.setTimeClockIp(saveChainNumVo.getTimeClockIp());
        saveBean.setUserName(saveChainNumVo.getUserName());
        saveBean.setPassword(saveChainNumVo.getPassword());
        return this.save(saveBean);
    }

    /**
     * 获取硬件清单
     * @return
     */
    public List<JSONObject> getHardwareList() {
		LambdaQueryWrapper<BaseTimeClock> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseTimeClock.class);
		lambdaQueryWrapper.eq(BaseTimeClock::getState, HardwareStateEnum.INUSE.getCode());
        return baseTimeClockService.list(lambdaQueryWrapper).stream().map(i -> {
            JSONObject resultBean = new JSONObject();
            resultBean.put("baseId",String.valueOf(i.getId()));
            resultBean.put("manufacturer",i.getManufacturer());
            resultBean.put("hardwareName",i.getName());
            return resultBean;
        }).collect(Collectors.toList());
    }

    /**
     * 查询硬件基本信息
     * @param baseIds
     * @return
     */
    public Object getHardwareBase(String baseIds) {
        List<String> baseList = Arrays.asList(baseIds.split(","));
        LambdaQueryWrapper<BaseTimeClock> baseTvBoxCondition = new QueryWrapper<BaseTimeClock>().lambda()
                .in(CollectionUtil.isNotEmpty(baseList),BaseTimeClock::getId,baseList);
        List<BaseTimeClock> baseTvBoxList = baseTimeClockService.list(baseTvBoxCondition);
        return baseTvBoxList;
    }

    /**
     * 查询硬件快照信息
     * @param codeId
     * @return
     */
    public Object getHardwareInfo(String codeId) {
        return this.getById(codeId);
    }

    /**
     * 获取所有打卡机
     * @return
     */
    @Override
    public List<CodeTimeClock> getTimeClockList() {
        LambdaQueryWrapper<CodeTimeClock> queryWrapper = new QueryWrapper<CodeTimeClock>().lambda();
        List<CodeTimeClock> codeTimeClocks = baseMapper.selectList(queryWrapper);
        return codeTimeClocks;
    }
}
